﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Xiaov.SmartWeb.Model;
using Xiaov.SmartWeb.Infrastructure.ExtMethod;
using Xiaov.SmartWeb.Model.ActiveRecord;
using System.Text;

namespace Xiaov.SmartWeb.Application.PluginSrc.StatisticalSystem.ReportDesign
{
  public partial class Admin_ExportXls : BasePage
  {
    public string DWBM { get; set; }
    public string DYTBDWB_BBBMS { get; set; }
    protected void Page_Load(object sender, EventArgs e)
    {
      PM_Filter_AbsolutePath();
      AjaxPro.Utility.RegisterTypeForAjax(typeof(Admin_ExportXls));
      if (!IsPostBack)
      {
        if (Request["dwbm"].ToRequestString() == "")
        {
          DWBM = PUser.USERDEPT;
        }
        else
          DWBM = Request.QueryString["dwbm"].ToRequestString();
        tb_nf.Text = DateTime.Now.AddMonths(-1).Year.ToString();
        tb_yf.Text = DateTime.Now.AddMonths(-1).Month.ToString();
        BBDYTBDW model = new BBDYTBDW();
        IList<string> list = model.FindAllByCache().Where(s => s.BBBM_DWBM.DWBM == DWBM).Select(s => s.BBBM_DWBM.BBBM).ToList();
        DYTBDWB_BBBMS = list.ToJoinString(',');
      }
    }
    [AjaxPro.AjaxMethod]
    public bool Export(object bbbms, object dwbm, object year, object month)
    {

      bool rtn = false;
      if (bbbms.ToRequestString().Length > 1)
      {
        bbbms = bbbms.ToRequestString().Remove(bbbms.ToRequestString().Length - 1);

        //Hack:导出Excel 补充方法需要陈君提供方法 注意BBBMS中
        string[] b = bbbms.ToRequestString().Split(',');
        StringBuilder xml = new StringBuilder();
        xml.Clear();
        System.Web.HttpContext.Current.Response.Clear();
        System.Web.HttpContext.Current.Response.Buffer = true;
        System.Web.HttpContext.Current.Response.ContentEncoding = Encoding.GetEncoding("utf-8");
        System.Web.HttpContext.Current.Response.Charset = "";
        System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + "a.xls");
        System.Web.HttpContext.Current.Response.ContentType = "text/xml";
        Excel.ExportExcel excel = new Excel.ExportExcel();
        xml.Append(excel.GetXML(dwbm.ToString(), bbbms.ToRequestString(), year.ToString(), month.ToString()));
        System.Web.HttpContext.Current.Response.Write(xml.ToString());
        //System.Web.HttpContext.Current.Response.End();


        //EXCELEXPORT model = new EXCELEXPORT();
        //model.CompositeKey = new EXCELEXPORT_CompositeKey();
        //model.CompositeKey.NF = year.ToInt32();
        //model.CompositeKey.YF = month.ToInt32();
        //model.CompositeKey.DWBM = dwbm.ToRequestString();
        //if (EXCELEXPORT.Exists(model.CompositeKey))
        //{
        //  model.Delete();
        //}
        //model.STATUS = 0;
        //model.BBBMLIST = bbbms.ToString();
        //model.Save();
        //model.RefreshCache();
        //导出Excel结束
        rtn = true;
      }
      return rtn;
    }

    [AjaxPro.AjaxMethod]
    public string DownFile(object dwbm, object year, object month)
    {
      string sql_download = "select status from excelexport where dwbm = '" + dwbm.ToRequestString() + "' and nf = " + year.ToRequestString() + " and yf = " + month.ToRequestString() + "";
      string dwmc = DWBMB.Find(dwbm).DWMC;

      object o = Maticsoft.DBUtility.DbHelperOra.GetSingle(sql_download);
      if (o.ToRequestString() == "1")
      {
        string file = Server.MapPath("/DownLoadExcel") + "/" + dwmc + "/" + dwmc + "_" + year + "_" + month + ".xls";
        if (System.IO.File.Exists(file))
        {
          return "/DownLoadExcel" + "/" + dwmc + "/" + dwmc + "_" + year + "_" + month + ".xls";
        }
        else
          return "#";
      }
      else
        return "#";
    }

    protected void tb_yf_TextChanged(object sender, EventArgs e)
    {

    }

    protected void tb_nf_TextChanged(object sender, EventArgs e)
    {

    }
  }
}